﻿using Elderlyhealth.DAL;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class AlertsCategoryList : System.Web.UI.UserControl
{
    private DataClasses1DataContext db;

    protected void Page_Load(object sender, EventArgs e)
    {
        // 初始化 DataContext 对象
        db = new DataClasses1DataContext();

        if (!IsPostBack)
        {
            BindAlertCategories();
        }
    }

    private void BindAlertCategories() 
    {
        using (var db = new DataClasses1DataContext())
        {
            var alertCategories = from alert in db.Talerts警报
                                  group alert by alert.警报类型 into grouped
                                  select new
                                  {
                                      CategoryName = grouped.Key,
                                      AlertCount = grouped.Count()
                                  };

            Repeater1.DataSource = alertCategories.ToList();
            Repeater1.DataBind();
        }
    }

    public IList GetAlertDetails(string categoryName)
{
    using (var db = new DataClasses1DataContext())
    {
            
                var alertDetails = from alert in db.Talerts警报
                                   where alert.警报类型 == categoryName
                                   select new
                                   {
                                       alert.警报id, // 确保包含警报ID
                                       alert.old_id, // 老人 ID
                                       alert.描述,
                                       alert.时间,
                                       alert.状态
                                   };


                return alertDetails.ToList();
    }
}



    protected void Page_Unload(object sender, EventArgs e)
    {
        if (db != null)
        {
            db.Dispose();
        }
    }

    protected void btnProcess_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        int alertId = Convert.ToInt32(btn.CommandArgument);

        using (var db = new DataClasses1DataContext())
        {
            var alert = db.Talerts警报.FirstOrDefault(a => a.警报id == alertId);
            if (alert != null && alert.状态 == "待处理")
            {
                alert.状态 = "已处理";
                db.SubmitChanges();

                // 可以选择在这里刷新页面或重新绑定数据，以反映最新状态
                BindAlertCategories(); // 重新绑定警报列表，确保前端显示最新状态
            }
        }
    }
   /* protected void ProcessAlert_Click(object sender, EventArgs e)
    {
        Button btnProcess = (Button)sender;
        int alertId = Convert.ToInt32(btnProcess.CommandArgument);

        using (var db = new DataClasses1DataContext())
        {
            var alert = db.Talerts警报.FirstOrDefault(a => a.警报id == alertId);
            if (alert != null)
            {
                alert.状态 = "已处理";
                db.SubmitChanges();
            }
        }

        // 重新绑定数据，更新显示状态
        BindAlertCategories();
    }
*/

  /*  protected void btnProcess_Click1(object sender, EventArgs e)
    {

    }*/

    protected void btnProcess_Click2(object sender, EventArgs e)
    {
        Button btnProcess = (Button)sender;
        int alertId = Convert.ToInt32(btnProcess.CommandArgument);

        using (var db = new DataClasses1DataContext())
        {
            var alert = db.Talerts警报.FirstOrDefault(a => a.警报id == alertId);
            if (alert != null)
            {
                alert.状态 = "已处理";
                db.SubmitChanges();
            }
        }

        // 重新绑定数据，更新显示状态
        BindAlertCategories();
    }
}